var ctx;
var selectSpec = [];
var count = $("#count").val();
$(function(){
	count = $("#count").val();
	// 默认选择一个类别
	$('.specBtn').each(function(){
		var defaultSpec = $('defaultSpec').val();
		var param = $(this).attr('id').split('_');
		if('true' == param[1]) {
			selectSpec.push(param[0]);
		}
	});
	$('.specBtn').click(function(){
		$(this).addClass('specBtnSec');
		$(this).siblings().removeClass('specBtnSec');
		$(this).siblings().each(function(){
			var param = $(this).attr('id').split('_');
			selectSpec.remove(param[0])
		});
		selectSpec.push( $(this).attr('id').split('_')[0]);
		// 刷新界面
		var goodsId = $('#goodsId').val();
		var attrId = selectSpec.toString();
		window.location.href = ctx + '/GoodsDetail?id=' + goodsId + '&attrId=' + attrId;
	})
	
	$(".wrap-bar li").click(function(){
		var index=$(this).index();
		$(this).css({"backgroundColor":"#fff","color":"#000"}).siblings().css({"backgroundColor":"#89C9D5","color":"#fff"})
		$(".wrap-details").eq(index).css("display","block").siblings().css("display","none");
	})
	
	$(".decrement").click(function() {
		if (count <= 0) {
			return false;
		}
		count--;
		$("#count").val(count);
	});

	$(".increment").click(function() {
		count++;
		$("#count").val(count);
	});

    $('.small-pic').slick({
		slidesToShow : 5,
		slidesToScroll : 1,
		autoplay: false,
  	    autoplaySpeed: 2000,
	});
});

function setCtx(ctx) {
    this.ctx = ctx;
}
function goodsDetail(id){
	window.location.href = ctx +"/integralDetail?id=" + id;
}

function showParam(id){
	window.location.href = ctx +"/showIntegralParam?id=" + id;
}

function goodsComments(id){
	window.location.href = ctx +"/integralGoodsComments?id=" + id;
}

function searchGoodsOrShops(){
	var selectType = $("#selectType").val();
	var name = $("#name").val();
	window.location.href = ctx +"/searchGoodsOrShops?pNo=1" + "&selectType=" +selectType +"&name=" + name;
}


function checkIntegralEnough(type) {
	if (2 == type) {
		var userIntegral = $("#userIntegral").val();
		var needIntegral = $("#needIntegral").val();
		if (needIntegral * count > userIntegral) {
			layer.tips('您的积分不够!', '#count', {
				tips : 3,
				time : 1000
			});
			return false;
		} else {
			return true;
		}
	} else {
		return true;
	}
}

function checkCount(count) {
	if (count == null || count == undefined || count == 0 || count == "") {
		layer.tips('请选择数量!', '#count', {
			tips : 3,
			time : 1000
		});
		return false;
	} else {
		return true;
	}
}

function duiHuan() {
	var goodsId = $("#goodsId").val();
	var shopId = $("#shopId").val();
	var goodsName = $("#goodsName").val();
	var logo = $("#goodsLogo").val();
	var price = $("#price").val();
	var type = $("#type").val();
	var goodsSpecId = $("#goodsSpecId").val();
	count = $('#count').val();
	if (checkCount(count) && checkIntegralEnough(type)) {
	    $.ajax({
            type: "POST",
            url: ctx + '/shopcart/checkBuyBefore',
            dataType : 'json',
            data:{
                goodsId:goodsId,
                shopId:shopId,
                goodsName:goodsName,
                price:price,
                type:type,
                count:count,
                goodsSpecId:goodsSpecId
            },
            success: function(data){
                if(data.success) {
                    window.location.href = ctx + "/shopcart/confirmOrder?goodsId="
                        + goodsId + "&shopId=" + shopId + "&goodsName=" + goodsName
                        + "&logo=" + logo + "&price=" + price + "&type=" + type
                        + "&count=" + count + "&goodsSpecId=" + goodsSpecId; 
                } else {
                    layer.msg(data.msg,{
                        icon: 1,
                        time: 1000 //2秒关闭（如果不配置，默认是3秒）
                    }); 
                }
            },
            error: function(res){
            }
        });
		
	}

}

//加入购物车
function addToCart(goodsId,shopId,goodsName,logo,price,type,count){
	count = $("#count").val();
	$.post(ctx +"/shopcart/addToCart",
			{goodsId:goodsId,
		     shopId:shopId,
		     goodsName:goodsName,
		     logo:logo,
		     price:price,
		     type:type,
		     count:count,
		     goodsSpecId:$("#goodsSpecId").val()
			},function(data){
				data = JSON.parse(data);
				if(data.success){
					layer.msg("加入购物车成功",{
						icon: 1,
						time: 1000 //2秒关闭（如果不配置，默认是3秒）
					});
				} else {
				    layer.msg(data.msg,{
                        icon: 1,
                        time: 1000 //2秒关闭（如果不配置，默认是3秒）
                    });
				}
			});
}

//加入收藏
function addMyFavorite(shopcartId, goodsId) {
        $.post(ctx + "/shopcart/addMyFavorite", {
            shopcartId : shopcartId,
            goodsId : goodsId
        }, function(data) {
        	data = JSON.parse(data);
            if (data.success) {
                layer.msg(data.msg, {
                    icon : 1,
                    time : 1000
                //2秒关闭（如果不配置，默认是3秒）
            	});
            } else {
                layer.msg(data.msg, {
                    icon : 2,
                    time : 1000
                //2秒关闭（如果不配置，默认是3秒）
                });
            }
        });
        layer.close(index);
}